package com.ewanghuiju.app.widget.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.ui.my.activity.ChangeBodyWithCodeActivity;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.pswkeyboard.KeyBoardAdapter;
import com.ewanghuiju.app.widget.pswkeyboard.OnPasswordInputFinish;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.autosize.AutoSize;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author sj  qq:751943509
 * @class 密码支付弹框
 * @time 2020/2/19
 */
public class PswKeyboardPopup extends BasePopupWindow {
    @BindView(R.id.popup_close)
    ImageView popupClose;
    @BindView(R.id.tv_pass1)
    TextView tvPass1;
    @BindView(R.id.img_pass1)
    ImageView imgPass1;
    @BindView(R.id.tv_pass2)
    TextView tvPass2;
    @BindView(R.id.img_pass2)
    ImageView imgPass2;
    @BindView(R.id.tv_pass3)
    TextView tvPass3;
    @BindView(R.id.img_pass3)
    ImageView imgPass3;
    @BindView(R.id.tv_pass4)
    TextView tvPass4;
    @BindView(R.id.img_pass4)
    ImageView imgPass4;
    @BindView(R.id.tv_pass5)
    TextView tvPass5;
    @BindView(R.id.img_pass5)
    ImageView imgPass5;
    @BindView(R.id.tv_pass6)
    TextView tvPass6;
    @BindView(R.id.img_pass6)
    ImageView imgPass6;
    @BindView(R.id.gv_keybord)
    GridView gvKeybord;

    private int currentIndex = -1;    //用于记录当前输入密码格位置

    private ArrayList<Map<String, String>> valueList;    //有人可能有疑问，为何这里不用数组了？
    private Context mContext;

    private TextView[] tvList;      //用数组保存6个TextView，为什么用数组？

    private ImageView[] imgList;      //用数组保存6个TextView，为什么用数组？

    public PswKeyboardPopup(Context context) {
        super(context);
        setWidth(getScreenWidth());
        this.mContext = context;
        ButterKnife.bind(this, getContentView());

        valueList = new ArrayList<>();
        initView();
        initValueList();

        setupView();
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_psw_keyboard);
    }


    private void initView() {
        tvList = new TextView[6];
        imgList = new ImageView[6];
        tvList[0] = tvPass1;
        tvList[1] = tvPass2;
        tvList[2] = tvPass3;
        tvList[3] = tvPass4;
        tvList[4] = tvPass5;
        tvList[5] = tvPass6;

        imgList[0] = imgPass1;
        imgList[1] = imgPass2;
        imgList[2] = imgPass3;
        imgList[3] = imgPass4;
        imgList[4] = imgPass5;
        imgList[5] = imgPass6;

    }

    private void initValueList() {

        // 初始化按钮上应该显示的数字
        for (int i = 1; i < 13; i++) {
            Map<String, String> map = new HashMap<>();
            if (i < 10) {
                map.put("name", String.valueOf(i));
            } else if (i == 10) {
                map.put("name", ".");
            } else if (i == 11) {
                map.put("name", String.valueOf(0));
            } else if (i == 12) {
                map.put("name", "");
            }
            valueList.add(map);
        }
    }

    private void setupView() {

        // 这里、重新为数字键盘gridView设置了Adapter
        KeyBoardAdapter keyBoardAdapter = new KeyBoardAdapter(mContext, valueList);
        gvKeybord.setAdapter(keyBoardAdapter);

        gvKeybord.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 11 && position != 9) {    //点击0~9按钮

                    if (currentIndex >= -1 && currentIndex < 5) {      //判断输入位置————要小心数组越界
                        ;
                        ++currentIndex;
                        tvList[currentIndex].setText(valueList.get(position).get("name"));

                        tvList[currentIndex].setVisibility(View.INVISIBLE);
                        imgList[currentIndex].setVisibility(View.VISIBLE);
                    }
                } else {
                    if (position == 11) {      //点击退格键
                        if (currentIndex - 1 >= -1) {      //判断是否删除完毕————要小心数组越界

                            tvList[currentIndex].setText("");

                            tvList[currentIndex].setVisibility(View.VISIBLE);
                            imgList[currentIndex].setVisibility(View.INVISIBLE);

                            currentIndex--;
                        }
                    }
                }
            }
        });
    }

    //设置监听方法，在第6位输入完成后触发
    public void setOnFinishInput(final OnPasswordInputFinish pass) {


        tvList[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().length() == 1) {

                    String strPassword = "";     //每次触发都要先将strPassword置空，再重新获取，避免由于输入删除再输入造成混乱

                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }
                    pass.inputFinish(strPassword);    //接口中要实现的方法，完成密码输入完成后的响应逻辑
                }
            }
        });
    }


    @OnClick({R.id.popup_close, R.id.tv_forget_paypwd})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.popup_close:
                dismiss();
                break;
            case R.id.tv_forget_paypwd:
                //跳转到支付密码界面
                new StartActivityUtil(mContext, ChangeBodyWithCodeActivity.class).putExtra(Constants.SETTING_TYPE, 2)
                        .startActivity(true);
                break;
            default:
                break;
        }
    }
}
